package com.hexinfo.dmpro.component.code.detection.utils;

import com.google.gson.Gson;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JsonReadUtil {

    /**
     * @Method readConfig
     * @Param
     * @param filePath
     * @Return java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>>
     * @Description 根据静态资源文件中的JSON文件读取配置
     * @Author Wang zhihao
     * @Date 2023/11/24 13:15
     * @Version V1.0
     */
    public static Map<String, Map<String, String>> readStaticConfig(String filePath) {
        Gson gson = new Gson();
        try {
            Resource resource = new ClassPathResource(filePath);
            FileSystemResource fileSystemResource = new FileSystemResource(resource.getFile());
            BufferedReader bufferedReader = new BufferedReader(new FileReader(fileSystemResource.getFile()));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            bufferedReader.close();
            String jsonString = stringBuilder.toString();
            Map<String, Map<String, String>> result = gson.fromJson(jsonString, Map.class);
            return result;
        } catch (Exception e) {
            throw new RuntimeException("从JSON文件中读取Map信息异常" + e.getMessage(), e);
        }
    }

    /**
     * @Method readLocalConfig
     * @Param
     * @param filePath
     * @Return java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>>
     * @Description 根据本地JSON文件读取配置
     * @Author Wang zhihao
     * @Date 2023/11/28 11:26
     * @Version V1.0
     */
    public static Map<String, Map<String, Map<String, String>>> readLocalConfig(String filePath) {
        Gson gson = new Gson();
        try {
            StringBuilder contentBuilder = new StringBuilder();
            try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
                String line;
                while ((line = br.readLine()) != null) {
                    contentBuilder.append(line).append("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return gson.fromJson(contentBuilder.toString(), Map.class);
        } catch (Exception e) {
            throw new RuntimeException("从JSON文件中读取Map信息异常" + e.getMessage(), e);
        }
    }

    public static Map<String, Map<String, Map<String, Object>>> readLocalConfigObject(String filePath) {
        Gson gson = new Gson();
        try {
            StringBuilder contentBuilder = new StringBuilder();
            try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
                String line;
                while ((line = br.readLine()) != null) {
                    contentBuilder.append(line).append("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return gson.fromJson(contentBuilder.toString(), Map.class);
        } catch (Exception e) {
            throw new RuntimeException("从JSON文件中读取Map信息异常" + e.getMessage(), e);
        }
    }

    /**
     * @Method readConfigByCluster
     * @Param
     * @param filePath
     * @param clusterName
     * @Return java.util.Map<java.lang.String,java.lang.String>
     * @Description 根据JSON获取指定集群信息
     * @Author Wang zhihao
     * @Date 2023/11/24 13:19
     * @Version V1.0
     */
    public static Map<String, Map<String, String>> readConfigByCluster(String filePath, String clusterName) {
        Map<String, Map<String, Map<String, String>>> paramsConfig = readLocalConfig(filePath);
        return paramsConfig.get(clusterName);
    }

    /**
     * @Method readConfigByClusterObject
     * @Param
     * @param filePath
     * @param clusterName
     * @Return java.util.Map<java.lang.String,java.lang.Object>
     * @Description 根据JSON获取Object类型的指定集群信息
     * @Author Wang zhihao
     * @Date 2023/12/26 15:23
     * @Version V1.0
     */
    public static Map<String, Map<String, Object>> readConfigByClusterObject(String filePath, String clusterName) {
        Map<String, Map<String, Map<String, Object>>> paramsConfig = readLocalConfigObject(filePath);
        return paramsConfig.get(clusterName);
    }

    @SuppressWarnings("unchecked")
    public static Map<String, String> castMap(Object obj) {
        return (Map)obj;
    }

    public static <T> List<T> castList(Object obj, Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }

    /**
     * @Method readAppointConfig
     * @Param
     * @param filePath
     * @param clusterName
     * @param appoint
     * @Return java.util.Map<java.lang.String,java.lang.String>
     * @Description 根据JSON获取集群下指定的信息
     * @Author Wang zhihao
     * @Date 2023/11/28 15:58
     * @Version V1.0
     */
    public static Map<String, String> readAppointConfig(String filePath, String clusterName, String appoint) {
        return readConfigByCluster(filePath, clusterName).get(appoint);
    }
}
